package com.crayfish.quduo.ui.activity;

import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.crayfish.quduo.R;
import com.crayfish.quduo.base.BaseMvpActivity;
import com.crayfish.quduo.commom.Params;
import com.crayfish.quduo.model.NewsBean;
import com.crayfish.quduo.model.UserBean;
import com.crayfish.quduo.presenter.MyNewsPresenter;
import com.crayfish.quduo.presenter.contract.MyNewsContract;
import com.crayfish.quduo.ui.adapter.NewsAdapter;
import com.crayfish.quduo.utils.SharedPreferencesUtil;
import com.crayfish.quduo.utils.ToastUtil;
import com.crayfish.quduo.widget.RecycleViewDivider;

import java.util.List;

import butterknife.BindView;

/**
 * 作者：crayfish(徐杰)
 * 描述：
 * Created by crayfish on 2017/3/30.
 */
public class MyNewsActivity extends BaseMvpActivity<MyNewsPresenter> implements MyNewsContract.View{

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;

    private NewsAdapter newsAdapter;
    private Boolean isRefresh = false;
    private UserBean user = null;

    @Override
    protected MyNewsPresenter createPresenter() {
        return new MyNewsPresenter();
    }

    @Override
    protected void initEventAndData() {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        user = SharedPreferencesUtil.getUser(this);
        if(user == null){
            finish();
            startMyActivity(LoginActivity.class);
            return;
        }
        swipeRefreshLayout.setColorSchemeColors(Color.BLUE,Color.GREEN,Color.YELLOW,Color.RED);
        swipeRefreshLayout.setDistanceToTriggerSync(300);
        swipeRefreshLayout.setProgressBackgroundColorSchemeColor(Color.WHITE);
        swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //检查是否处于刷新状态
                if (!isRefresh) {
                    isRefresh = true;
                    //模拟加载网络数据，这里设置4秒，正好能看到4色进度条
                    new Handler().postDelayed(new Runnable() {
                        public void run() {

                            //显示或隐藏刷新进度条
                            swipeRefreshLayout.setRefreshing(false);
                            //修改adapter的数据
                            mPresenter.getMyNews(user.getObjectId());
                            isRefresh = false;
                        }
                    }, 3000);
                }
            }
        });
        final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new RecycleViewDivider(this,LinearLayoutManager.VERTICAL));
        recyclerView.setHasFixedSize(true);//固定高度提高性能
        newsAdapter = new NewsAdapter();
        newsAdapter.setOnRecyclerViewItemClickListener(new NewsAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, NewsBean item) {
                Intent intent = new Intent(MyNewsActivity.this, NewsDetailsActivity.class);
                intent.putExtra(Params.CONTENT,item.getContent());
                startActivity(intent);
            }
        });
        recyclerView.setAdapter(newsAdapter);
        mPresenter.getMyNews(user.getObjectId());
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_list;
    }

    @Override
    public void showError(String msg) {
        ToastUtil.show(msg);
    }

    @Override
    public void showMyNews(List<NewsBean> newsBeans) {
        if(newsBeans.size() > 0){
            newsAdapter.clear();
            newsAdapter.addAll(newsBeans);
        }
    }
}
